/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmProcesoElectoral.java
 *
 * Created on 19/05/2011, 01:01:20 PM
 */
package verins.GUI;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import verins.BO.EstadoProcesoElectoralBO;
import verins.BO.ProcesoElectoralBO;
import verins.BO.ProcesoEmpadronamientoBO;
import verins.BO.TipoProcesoElectoralBO;
import verins.beans.Estadoprocesoelectoral;
import verins.beans.Procesoelectoral;
import verins.beans.Procesoempadronamiento;
import verins.beans.Tipoprocesoelectoral;
import verins.beans.Usuario;
/**
 *
 * @author jpenam
 */
public class frmProcesoElectoral extends JDialog {
     private Procesoelectoral procesoElectoral =null;
     private List lstProcEmpad=null;
     private Usuario usuario=null;
    /** Creates new form frmProcesoElectoral */ 
    public frmProcesoElectoral(Procesoelectoral proceso,JDialog ventana,Usuario user) {
        super(ventana,true);
        initComponents();
        verins.Utils.Comunes.centrarVentana(this);
        procesoElectoral=proceso;
        usuario=user;
        /*******restricciones********/
        setearValidadores();
        llenarCombos();
        if(proceso != null){
            mostrarDatos();
            lstProcEmpad=ProcesoEmpadronamientoBO.finByProcElectoral(procesoElectoral.getIdprocesoelectoral());
            if (procesoElectoral.getIdestadoprocesoelectoral().getIdestadoprocesoelectoral()>1){
                lnkAsignar.setVisible(false);
                lnkProcEmpad.setVisible(false);
                btnGuardar.setVisible(false);
            }
        }else{//nuevo
             lnkProcEmpad.setVisible(false);
             lnkAsignar.setVisible(false);
        }   
    }
    
     private void setearValidadores(){
        txtNombre.setDocument(new verins.Utils.LimitInput(45));
        txtNombre.addKeyListener(verins.Utils.Verificador.verificaSoloCaracteresValidos());
        txtCantidad.setDocument(new verins.Utils.LimitInput(6));
        txtCantidad.addKeyListener(verins.Utils.Verificador.verificaSoloNumeros());
    }
     
     private void llenarCombos(){
        List lista=null;
        lista=EstadoProcesoElectoralBO.findAll();
        verins.Utils.Comunes.llenarCombo(cmbEstado,lista);
        lista=TipoProcesoElectoralBO.findAll();
        verins.Utils.Comunes.llenarCombo(cmbtipo, lista);
    }
     private Procesoelectoral obtenerDatos(){
        Procesoelectoral proceso=null;
        //valida
        if (procesoElectoral==null){
            proceso=new Procesoelectoral();
            proceso.setIdprocesoelectoral(0);
            proceso.setNombre(txtNombre.getText());
            proceso.setFechainicio(datFechaIni.getDate());
            proceso.setFechafin(dateFechafin.getDate());
            proceso.setIdestadoprocesoelectoral((Estadoprocesoelectoral)cmbEstado.getSelectedItem());
            proceso.setIdtipoprocesoelectoral((Tipoprocesoelectoral)cmbtipo.getSelectedItem());
            proceso.setPoblacion(Integer.parseInt(txtCantidad.getText()));
            
            
            procesoElectoral=proceso;
        }else{
           procesoElectoral.setNombre(txtNombre.getText());
            procesoElectoral.setFechainicio(datFechaIni.getDate());
            procesoElectoral.setFechafin(dateFechafin.getDate());
            procesoElectoral.setIdestadoprocesoelectoral((Estadoprocesoelectoral)cmbEstado.getSelectedItem());
            procesoElectoral.setIdtipoprocesoelectoral((Tipoprocesoelectoral)cmbtipo.getSelectedItem());
            procesoElectoral.setPoblacion(Integer.parseInt(txtCantidad.getText()));
           
        }
        return procesoElectoral;
    }
     
     private void mostrarDatos(){
        txtNombre.setText(procesoElectoral.getNombre());
        cmbtipo.setSelectedItem(procesoElectoral.getIdtipoprocesoelectoral());
        cmbEstado.setSelectedItem(procesoElectoral.getIdestadoprocesoelectoral());
        datFechaIni.setDate(procesoElectoral.getFechainicio());
        dateFechafin.setDate(procesoElectoral.getFechafin());
        txtCantidad.setText(procesoElectoral.getPoblacion().toString());
     }
      public void bloquecamposMostrar(){
        txtNombre.setEnabled(false);
        cmbtipo.setEnabled(false);
        cmbEstado.setEnabled(false);
        datFechaIni.setEnabled(false);
        dateFechafin.setEnabled(false);
        txtCantidad.setEnabled(false);
    }
      public void bloquecamposEditar(){
        txtNombre.setEnabled(false);
        cmbtipo.setEnabled(false);
        cmbEstado.setEnabled(true);
        datFechaIni.setEnabled(true);
        dateFechafin.setEnabled(true);
        //txtCantidad.setEnabled(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        cmbtipo = new javax.swing.JComboBox();
        datFechaIni = new com.toedter.calendar.JDateChooser();
        dateFechafin = new com.toedter.calendar.JDateChooser();
        cmbEstado = new javax.swing.JComboBox();
        lnkAsignar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtCantidad = new javax.swing.JTextField();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lnkProcEmpad = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Proceso electoral");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos proceso electoral"));
        jPanel1.setToolTipText("");

        jLabel1.setText("Nombre Proceso Electoral :");

        jLabel2.setText("Tipo Proceso Electoral :");

        jLabel3.setText("Fecha Inicio :");

        jLabel4.setText("Fecha Fin:");

        jLabel5.setText("Estado :");

        txtNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombreActionPerformed(evt);
            }
        });

        datFechaIni.setDateFormatString("dd-MM-yyyy");

        dateFechafin.setDateFormatString("dd-MM-yyyy");

        lnkAsignar.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        lnkAsignar.setForeground(java.awt.Color.blue);
        lnkAsignar.setText("Asignar ubigeo");
        lnkAsignar.setBorderPainted(false);
        lnkAsignar.setContentAreaFilled(false);
        lnkAsignar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lnkAsignar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lnkAsignarActionPerformed(evt);
            }
        });

        jLabel6.setText("Cantidad Minima Firmas :");

        txtCantidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCantidadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(194, Short.MAX_VALUE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(cmbEstado, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(dateFechafin, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(datFechaIni, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cmbtipo, javax.swing.GroupLayout.Alignment.LEADING, 0, 141, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 134, Short.MAX_VALUE)
                            .addComponent(lnkAsignar, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(24, 24, 24)))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cmbtipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(datFechaIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(dateFechafin, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(lnkAsignar, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        lnkProcEmpad.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        lnkProcEmpad.setForeground(java.awt.Color.blue);
        lnkProcEmpad.setText("ver procesos de empadronamiento...");
        lnkProcEmpad.setBorderPainted(false);
        lnkProcEmpad.setContentAreaFilled(false);
        lnkProcEmpad.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lnkProcEmpad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lnkProcEmpadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lnkProcEmpad, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 175, Short.MAX_VALUE)
                        .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lnkProcEmpad, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelar)
                    .addComponent(btnGuardar))
                .addGap(28, 28, 28))
        );

        jPanel1.getAccessibleContext().setAccessibleName("Datos del proceso electoral");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombreActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNombreActionPerformed

     public void setearBotonSalirCancelar(String texto){
        btnCancelar.setText(texto);
    }
    
    public void setearBotonGuardarEditar(String texto){
        btnGuardar.setText(texto);
    }
    
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
         if (((JButton)evt.getSource()).getText().equalsIgnoreCase("cancelar")){
            if (verins.Utils.Mensajes.cancelar(evt,this)){
                this.dispose();
            }
         }else{
             dispose();
         }
    }//GEN-LAST:event_btnCancelarActionPerformed
 private boolean validarCampos(){
    boolean valido=true;
     if (txtNombre.getText().trim().length()==0) {
        verins.Utils.Mensajes.alerta("Debe ingresar nombre de proceso");
        return false;
    }
   if (txtCantidad.getText().trim().length()==0) {
        verins.Utils.Mensajes.alerta("Debe ingresar Cantidad minima en el proceso");
        return false;
    }
    if (datFechaIni.getDate()==null) {
        verins.Utils.Mensajes.alerta("Debe ingresar fecha de inicio");
        return false;
    }
    if (dateFechafin.getDate()==null) {
        verins.Utils.Mensajes.alerta("Debe ingresar fecha fin");
        return false;
    }
    if (dateFechafin.getDate().compareTo(datFechaIni.getDate())<0){
        verins.Utils.Mensajes.alerta("La fecha fin debe ser mayor a la fecha inicio ");
        return false;
    }
        
    return valido;
    }
    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        if (((JButton)evt.getSource()).getText().equalsIgnoreCase("guardar")){
             if(validarCampos()){
                if (verins.Utils.Mensajes.guardar(evt, this)){

                    procesoElectoral=obtenerDatos();
                    if(verins.Utils.Mensajes.exito(ProcesoElectoralBO.save(procesoElectoral,usuario.getIdusuario())))
                        this.dispose();
                }
            }
        }else{
        //editar
            setearBotonGuardarEditar("Guardar");
            setearBotonSalirCancelar("Cancelar");
            bloquecamposEditar();
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void lnkProcEmpadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lnkProcEmpadActionPerformed
        // TODO add your handling code here:
        frmProcesoEmpadronamiento frmEmpadron=null;
        lstProcEmpad=ProcesoEmpadronamientoBO.finByProcElectoral(procesoElectoral.getIdprocesoelectoral());
        if (lstProcEmpad!=null && lstProcEmpad.size()==1){
            frmEmpadron=new frmProcesoEmpadronamiento((Procesoempadronamiento)lstProcEmpad.get(0),procesoElectoral, null,usuario);
            frmEmpadron.bloquearCamposMostrar();
            frmEmpadron.setearBotonGuardarEditar("Editar");
            frmEmpadron.setearBotonSalirCancelar("Salir");
            frmEmpadron.setVisible(true);
            
            
        }else{//varios process o ninguno
           frmBusquedaProcesoEmpad frm=new frmBusquedaProcesoEmpad(procesoElectoral,null,usuario);
           frm.bloquearBtnBuscar();
           frm.setVisible(true);
        }
    }//GEN-LAST:event_lnkProcEmpadActionPerformed

    private void lnkAsignarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lnkAsignarActionPerformed
        // TODO add your handling code here:
        //abrir la otra ventana
        
        frmTipoParticipanteXProceso frm=new  frmTipoParticipanteXProceso(procesoElectoral,this);
        frm.setVisible(true);
        
    }//GEN-LAST:event_lnkAsignarActionPerformed

    private void txtCantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCantidadActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCantidadActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //new frmProcesoElectoral(null,this).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JComboBox cmbEstado;
    private javax.swing.JComboBox cmbtipo;
    private com.toedter.calendar.JDateChooser datFechaIni;
    private com.toedter.calendar.JDateChooser dateFechafin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton lnkAsignar;
    private javax.swing.JButton lnkProcEmpad;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
